<?php

/**
 * This is the model class for table "v_categories".
 *
 * The followings are the available columns in table 'v_categories':
 * @property string $id
 * @property string $name
 * @property string $parent
 * @property string $category_image_url
 * @property string $description
 * @property string $param
 * @property integer $ordering
 * @property integer $published
 */
class Categories extends CActiveRecord
{
	
	//public $pname;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'v_categories';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('name', 'unique', 'message'=>'分类名重复'),
			array('name, ordering', 'required'),
			array('ordering, published', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>200),
			array('parent', 'length', 'max'=>11),
			array('category_image_url, description, param', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, parent, category_image_url, description, param, ordering, published', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => '分类名',
			'parent' => '父分类名',
			'category_image_url' => '分类封面',
			'description' => '分类说明',
			'param' => 'Param',
			'ordering' => '排序',
			'published' => 'Published',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('parent',$this->parent,true);
		$criteria->compare('category_image_url',$this->category_image_url,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('param',$this->param,true);
		$criteria->compare('ordering',$this->ordering);
		$criteria->compare('published',$this->published);
		
		$criteria->addCondition("published = 1");
		$criteria->addCondition("ID != 1");
		
		/*
		$criteria=new CDbCriteria;
		$criteria->alias = 'c1';		
		$criteria->select = 'c1.* , c2.name as pname';
		$criteria->join =  'LEFT JOIN v_categories as c2 ON c1.parent = c2.id';
		*/
		
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination'=>array(
						'pageSize'=>10,
				),
		));
		
	}
	
	public function findCategorietName($pk)
	{
		$model = Categories::model()->findByPk($pk);
		return $model['name'];
		
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Categories the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
